package com.wlc.controller;

import com.wlc.pojo.PageResult;
import com.wlc.pojo.Result;
import com.wlc.pojo.User;
import com.wlc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据ID查询用户
     */
    @GetMapping("/{id}")
    public Result<User> getById(@PathVariable Long id) {
        log.info("根据ID查询用户: {}", id);
        try {
            User user = userService.findById(id);
            return Result.success(user);
        } catch (Exception e) {
            log.error("查询用户失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页查询用户列表
     */
    @GetMapping("/list")
    public Result<PageResult<User>> list(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String employeeName,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("分页查询用户列表: username={}, employeeName={}, page={}, size={}",
                username, employeeName, page, size);
        try {
            PageResult<User> pageResult = userService.findList(username, employeeName, page, size);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("查询用户列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新增用户
     */
    @PostMapping
    public Result<String> save(@RequestBody User user) {
        log.info("新增用户: {}", user.getUsername());
        try {
            userService.save(user);
            return Result.success("新增用户成功");
        } catch (Exception e) {
            log.error("新增用户失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新用户
     */
    @PutMapping
    public Result<String> update(@RequestBody User user) {
        log.info("更新用户: {}", user.getId());
        try {
            userService.update(user);
            return Result.success("更新用户成功");
        } catch (Exception e) {
            log.error("更新用户失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除用户: {}", id);
        try {
            userService.deleteById(id);
            return Result.success("删除用户成功");
        } catch (Exception e) {
            log.error("删除用户失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 启用/禁用用户
     */
    @PutMapping("/status/{id}")
    public Result<String> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        log.info("更新用户状态: id={}, status={}", id, status);
        try {
            userService.updateStatus(id, status);
            return Result.success("更新状态成功");
        } catch (Exception e) {
            log.error("更新用户状态失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}